# 设置代码目录和查找目录
set source_dir "SOURCE_DIR"
lappend search_path SOURCE_DIR

# 设置工作目录
set working_dir "WORKING_DIR"
cd $working_dir

# 定义输出文件路径
set report_dir "${working_dir}/report"
set result_dir "${working_dir}/result"
file mkdir $report_dir
file mkdir $result_dir
file mkdir ./log

# 设置顶层模块名称
set top_module "TOP_MODULE"

# 指定输入Verilog文件
set verilog_files [list \
    "TOP_MODULE_FILE" \
]

# 读取Verilog代码
foreach active_files $verilog_files {
  analyze -format verilog $active_files
}
elaborate $top_module
uniquify

# 如果没有指定db则设置目标库为GTECH通用库
if {[info exists ::env(db)] && ![string equal $::env(db) "off"]} {
  set target_library "$::env(db).db"
} else {
  set target_library "gtech.db"
}
set link_library "* $target_library"

# 如果有额外的时序约束文件，可以在此处添加
set clock_name "clk";
set reset_name "rst_n";
set clk_period 1; #1ns
set clk_uncertainty_setup [expr $clk_period/200];
set clk_latency [expr $clk_period/5];

create_clock -name $clock_name -period [expr $clk_period] [get_ports $clock_name]
set input_delay  [expr $clk_period/4];
set output_delay [expr $clk_period/4];
set allin_except_CLK [remove_from_collection [all_inputs] [get_ports clk]]
set_input_delay [expr $input_delay] -clock $clock_name $allin_except_CLK
set_output_delay [expr $output_delay] -clock $clock_name [all_outputs]

# 负载和驱动设置
set_driving_cell -lib_cell BUF2 [all_inputs]
set_load -pin_load 0.5 [all_outputs]
set_max_fanout 32 $top_module

# 设置输出的路径
set out_netlist         "$result_dir/$top_module.v";
set out_db              "$result_dir/$top_module.db";
set out_sdf             "$result_dir/$top_module.sdf";
set out_sdc             "$result_dir/$top_module.sdc";

# 执行初步综合
compile

# 报告设计信息
report_area > ${report_dir}/${top_module}_area.rpt
report_timing > ${report_dir}/${top_module}_timing.rpt
report_power > ${report_dir}/${top_module}_power.rpt
report_reference > ${report_dir}/${top_module}_reference.rpt
report_qor > ${report_dir}/${top_module}_qor.rpt

change_names -rule verilog -hier
write -format verilog -hierarchy -output $out_netlist
write -format ddc -hierarchy -output $out_db
write_sdf  $out_sdf
write_sdc  $out_sdc

# 检查环境变量gui是否设置为"off"
if {[info exists ::env(gui)] && [string equal $::env(gui) "off"]} {
  # 如果设置了 "no_gui"，则以 exit 结束
  puts "Exiting without starting GUI."
  exit
} else {
  # 否则启动 GUI
  puts "Starting GUI."
  start_gui
}
